'''
Created on April 12, 2011

@author: yc30v1m
'''
from logging import getLogger
import csv
import sys

log = getLogger(__name__)

class Reader(object):
    
    '''
    classdocs
    '''

    _fileName=None
    _fileInMemory=None
    
    #private
    __headerRow=[]
    __hasHeader = None
    __quotechar=None
    __doublequote=None
    __delim=None
    __skipinitialspace=None

    def __init__(self, fileName=None):
        if fileName!=None:
            self.setFileName(fileName)
            
    def setFileName(self,fileName):
        self._fileName=fileName
        self._fileInMemory = open(self._fileName, 'rU')
        self.snifFile()
        
    def processToListWithDictCols(self):
        try:
            #self._fileInMem.seek(0)
            f = open(self._fileName, 'rt')
            f.seek(0)
            self._reader = csv.DictReader(f, fieldnames=self.__headerRow)
            rows = []
            for r in self._reader:
                rows.append(r)
            return rows
        except:
            log.debug("Unable to process inputFile to Dict" + str(self._fileName))
            return False
    
    def hasHeader(self):
        return self.__hasHeader
    
    def snifFile(self):
        try:
            self._fileInMemory.seek(0)
            sniffer = csv.Sniffer()
            dialect = sniffer.sniff(self._fileInMemory.read(20480))
            self.__delim = dialect.delimiter
            self._fileInMemory.seek(0)
            self.__hasHeader = sniffer.has_header(self._fileInMemory.read(20480))
            self.__setHeaders(self.__hasHeader)

            ''' TODO
            delimeter = sniffer._guess_delimiter(self._fileInMemory.read(20480))
            print "Delimiter"
            print delimeter
              
            if delimeter:
                quotechar, doublequote, delim, skipinitialspace = sniffer._guess_quote_and_delimiter(self._fileInMem.read(20480), delimeter)
                self.__quotechar = quotechar
                self.__doublequote = doublequote
                self.__delim = delim
                self.__skipinitialspace = skipinitialspace
            '''
            
            
        except:
            log.debug("Unable to snifFile")
#            self.critical("Unable to snifFile")
            
    def __setHeaders(self, header):
        try:
            header=False
            self._fileInMemory.seek(0)
            reader = csv.reader(self._fileInMemory)
            firstRow = []
            self.__headerRow = []
            for row in reader:
                firstRow = row
            if header==True:
                self.__headerRow = firstRow
            else:
                colCount = len(firstRow)
                for col in range(colCount):
                    self.__headerRow.append('COLUMN-' + str(col + 1))
        except:
            log.debug("Unable to set Headers for File :" + str(self._fileName) )
